#coding:utf8
from bae.core import const
import MySQLdb

class DBHander:
	def __init__(self,dbname,host,port,user,pwd):
		self.dbname = dbname
		self.host = host
		self.port = port
		self.user = user
		self.pwd = pwd

	def __GetConnect(self):
		if not self.dbname:
			raise(NameError,"没有设置数据库信息")
		self.conn = MySQLdb.connect(\
			db = self.dbname,\
			host = self.host,\
			port = self.port,\
			user = self.user,\
			passwd = self.pwd,\
			charset="utf8")
		cur = self.conn.cursor()
		if not cur:
			raise(NameError,"连接数据库失败")
		else:
			return cur

	def ExecQuery(self,sql):
		cur = self.__GetConnect()
		cur.execute(sql)
		resList = cur.fetchall()
		self.conn.close()
		return resList

	def ExecNonQuery(self,sql):
		cur = self.__GetConnect()
		cur.execute(sql)
		self.conn.commit()
		self.conn.close()

try:
	dbh = DBHander(\
		dbname = 'sbWurMCXGdvrgouJLplJ',\
		host = const.MYSQL_HOST,\
		port = int(const.MYSQL_PORT),\
		user = const.MYSQL_USER,\
		pwd = const.MYSQL_PASS)
except:
	print '连接数据库错误!'